<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Goodshop extends Base{
    //收藏商品
    public function collectgood(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '商品id呢']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 504, 'msg' => 'openid呢']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $collect=Db::name('goodscollect')->where('cus_id',$cus_id)->where('openid',$openid)->where('good_id',$id)->find();
            if($collect){
                return json(['code' => 201, 'msg' => '您已收藏此商品！']);exit();
            }else{
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $data['cus_id']=$cus_id;
                    $data['good_id']=$id;
                    $data['openid']=$openid;
                    $data['state']=1;
                    $data['time']=time();
                    $enlistin=Db::name('goodscollect')->insert($data);
                    if($enlistin){
                        return json(['code' => 200, 'msg' => '收藏成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '收藏失败！']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $data['cus_id']=$cus_id;
                    $data['good_id']=$id;
                    $data['openid']=$openid;
                    $data['state']=1;
                    $data['time']=time();
                    $enlistin=Db::name('goodscollect')->insert($data);
                    if($enlistin){
                        return json(['code' => 200, 'msg' => '收藏成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '收藏失败！']);exit();
                    }
                }else{
                return json(['code' => 505, 'msg' => '未开启此平台']);exit();
                }
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //取消收藏
    public function canclecollect(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => 'openid呢']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 504, 'msg' => '收藏id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $data['state']=2;
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $collect=Db::name('goodscollect')->where('cus_id',$cus_id)->where('openid',$openid)->where('id',$id)->update($data);
                if($collect){
                    return json(['code' => 200, 'msg' => '成功']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $collect=Db::name('goodscollect')->where('cus_id',$cus_id)->where('openid',$openid)->where('id',$id)->update($data);
                if($collect){
                    return json(['code' => 200, 'msg' => '成功']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '失败']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //我的收藏
    public function mycollect(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => 'openid呢']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $collect=Db::name('goodscollect')->alias('a')->join('shopgoods b', 'a.good_id = b.id','LEFT')->field('a.id,a.good_id,b.title,b.img,b.price')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->where('a.state',1)->order('a.id desc')->select();
                if($collect){
                    for ($i=0; $i <count($collect) ; $i++) {
                        $collect[$i]['checked']=false;
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$collect]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $collect=Db::name('goodscollect')->alias('a')->join('shopgoods b', 'a.good_id = b.id','LEFT')->field('a.id,a.good_id,b.title,b.img,b.price')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->where('a.state',1)->order('a.id desc')->select();
                if($collect){
                    for ($i=0; $i <count($collect) ; $i++) {
                        $collect[$i]['checked']=false;
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$collect]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    public function iscollect(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '商品id呢']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => 'openid呢']);exit();
        }
        $collect=Db::name('goodscollect')->where('cus_id',$cus_id)->where('openid',$openid)->where('good_id',$id)->where('state',1)->find();
        if($collect){
            return json(['code' => 200, 'msg' => '您已收藏此商品！']);exit();
        }else{
            return json(['code' => 500, 'msg' => '未曾收藏！']);exit();
        }
    }
    public function backcustomer(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $all=Db::name('evaluate')->where('cus_id',$cus_id)->count();
        $info=Db::name('evaluate')->alias('a')->join('users b', 'a.openid = b.openid','LEFT')->field('a.*,b.nickname,b.avatar')->where('a.cus_id',$cus_id)->order('a.id desc')->find();
        $selfall=0;
        if($info){
            $info['img']=explode(',', $info['img']);
            $selfall=Db::name('evaluate')->where('cus_id',$cus_id)->where('openid',$info['openid'])->count();
        }
        return json(['code' => 200, 'dataall' => $all, 'info' => $info, 'selfall' => $selfall]);exit();
    }
    public function shopserver(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $info['flags']=array();
        $info['flagtitle']=array();
        $info['servershop']=array();
        $info['flagcontent']='';
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $info['flags']=explode(',', $customer['flags']);
            $info['flagtitle']=explode(',', $customer['flagtitle']);
            $info['servershop']=explode(',', $customer['servershop']);
            $info['flagcontent']=$customer['flagcontent'];
        }
        return json(['code' => 200, 'info' => $info]);exit();
    }
    public function shopbanner(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $shopbanner=Db::name('shopbanner')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->select();
        if($shopbanner){
            return json(['code' => 200,  'msg' => '成功', 'shopbanner' => $shopbanner]);exit();
        }
        return json(['code' => 500,  'msg' => '出错']);exit();
    }
}